package com.tomek.ktoryoperator.utils;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.tomek.ktoryoperator.services.UpdateService;

public class AlarmUtils {
	public static void manageAlarms(Context context) {
		AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

		PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, UpdateService.class),
				PendingIntent.FLAG_UPDATE_CURRENT);
		am.cancel(pi);

		LogUtils.d(AlarmUtils.class, "manageAlarms", "alarm cleared");

		if (PreferenceUtils.shouldRunInBackground(context)) {
			long interval = PreferenceUtils.getReadInterval(context);

			am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pi);

			LogUtils.d(AlarmUtils.class, "manageAlarms", "alarm set up: interval=%d", interval);
		}
	}
}